<template>
	<!-- <view class="login" v-show="is_show"> -->
	<view class="login" v-show="isShow" :style="'width:'+mobile.screenWidth+'px;height:'+mobile.screenHeight+'px'">
		<!-- 底部弹出框 -->
		<view :animation="animationData" class="bottom_box">
			<view class="title"><text class="titleText">推掌柜登录</text></view>
			
			<view class="tips_text">
				<view class="text">登录后该应用将获得以下权限</view>
				<!-- <image src="/static/images/dui.png" class="icon"></image> -->
				<view class="text">获得你的公开信息（昵称、头像等）</view>
			</view>
			
			<view class="buttonFooter">
				<view class="count" @tap="toIndex" style="border-radius: 10px;">拒绝</view>
				<view class="submit" @tap="bindGetUserInfo" style="border-radius: 10px;">允许</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		Index
	} from '../../apirequest/index-model.js';
	var i = new Index();
	export default {
		props:{
			is_show:{
				type:Boolean,
				default:true
			}
		},
		data() {
			return {
				animationData: {},
				showModalStatus: false,
				mobile:uni.getSystemInfoSync(),
				canIUse: uni.canIUse('button.open-type.getUserInfo'),
				isHide: false,
				user_bind_show: true,
				userinfo: '',
				site_config: {
					site_logo: ''
				},
				is_ok: 0,
				isShow:true
			}
		},
		onLoad: function(options) {},
		// onShow: function(){
		// 	if(uni.getStorageSync('userinfo')){
		// 		this.isShow = false
		// 	}else{
		// 		this.isShow = true
		// 	}
		// },
		methods: {
			change() {

			},
			bindClick(index) {
				
				this.list.splice(index, 1)
			},
			
			checkboxChange(e) {
					if (e.detail.value.length > 0) {
						this.is_ok = 1;
					} else {
						this.is_ok = 0;
					}
				},
				//返回上一页
				backTo: function() {
					uni.setStorageSync('login_refesh', 1);
					var pages = getCurrentPages(); //获取加载的页面
			
					var prevPage = pages[pages.length - 2];
					var prev_url = prevPage ? prevPage.route : ''; //上页面url
			
					if (!prevPage || prev_url == 'pages/login/login') {
						uni.reLaunch({
							url: '/pages/index/index'
						});
					} else {
						wx.navigateBack({
							delta: 1,
						});
					}
				},
			
				toIndex: function() {
					uni.reLaunch({
						url: '/pages/shop/index/index'
					});
				},
			
				_login: function(o) {
					var that = this;
					uni.login({
						success: (res) => {
							// 获取到用户的 code 之后：res.code
							console.log(res);
			
							let parent_id = uni.getStorageSync('pid') || 0;
							
							i.login({
									pid: parent_id,
									user_info: JSON.stringify(o.userInfo),
									avatarUrl: o.userInfo.avatarUrl,
									nickName: o.userInfo.nickName,
									iv: o.iv,
									encryptedData: encodeURIComponent(o.encryptedData),
									code: res.code
								},
								function(t) {
									if (t.code == 1) {
										uni.setStorageSync('userinfo', t.data);
										uni.setStorageSync('token', t.data.token);
										that.userinfo = t.data
										// if (t.data.mobile != '') {
											that.isShow = false;
											that.$emit('is_login',true)
			// 							} else {
			// 								that.user_bind_show = false
			// 								that.userinfo = t.data
			
			// 							}
									}
			
								},
								function(t) {
									uni.showToast({
										title: t.msg,
										icon: 'none'
									});
								}
							);
							
							
						}
					});
				},
			
				bindGetUserInfo: function(e) {
					// if(!this.is_ok){
					// 	uni.showToast({
					// 		title:"请阅读并同意《用户协议》",
					// 		icon:'none'
					// 	})
					// 	return false;
					// }
					var that = this;
					// #ifdef MP
						
						uni.getUserProfile({
							desc: '用于完善会员资料',
							success: function(res) {
								console.log('成功');
								if (res.errMsg == 'getUserProfile:ok') {
								// if (res.errMsg == 'getUserProfile:ok') {
									//用户按了允许授权按钮
									// 获取到用户的信息了
									that._login(res);
									
								} else {
									//用户按了拒绝按钮
									uni.showModal({
										title: '提示',
										content: res.errMsg,
										showCancel: false,
										confirmText: '返回授权',
										success: function(res) {
											// 用户没有授权成功
											if (res.confirm) {
												console.log('用户点击了“返回授权”');
											}
										}
									});
								}
							}
						});
					// #endif
					//#ifdef APP-PLUS
						uni.login({
								provider: 'weixin',
								// onlyAuthorize:true, 
								success: function(loginRes) {
							
									// 获取用户信息
									uni.getUserInfo({
										provider: 'weixin',
										success: function(infoRes) {
										
											let parent_id = uni.getStorageSync(
												'pid') || 0;
											i.login({
													pid: parent_id,
													user_info: infoRes.userInfo,
													type: 'app'
												}, 
												function(t) {
									
													if (t.code == 1) {
														uni.setStorageSync('userinfo',t.data);
														uni.setStorageSync('token', t.data.token);
														that.userinfo = t.data
														that.backTo();
													}
				
												},
												function(t) {
													uni.showToast({
														title: t.msg,
														icon: 'none'
													});
												}
											);
										},
										fail: function(res) {
										
										}
									});
									
								},
								fail: function(res) {
								
								}
						});
					//#endif
				
				},
				agree: function(e) {
					uni.navigateTo({
						url: '/pages/agreement/agreement?id=11'
					});
				},
				//获取手机号
				getPhoneNumber: function(o) {
					var n = this.currentPage;
					var that = this;
					if ('getPhoneNumber:fail user deny' == o.detail.errMsg) {
						uni.showModal({
							title: '提示',
							showCancel: false,
							content: '未授权'
						});
					} else {
						uni.showLoading({
							title: '授权中'
						});
			
						let user_id = that.userinfo.id;
						console.log(that.userinfo)
						i.take_mobile({
								user_id: user_id,
								iv: o.detail.iv,
								encryptedData: o.detail.encryptedData,
								sessionKey: that.userinfo.session_key
							},
							function(t) {
								uni.hideLoading();
								if (t.code == 1) {
									uni.setStorageSync('userinfo', t.data);
									that.backTo();
								} else {
									uni.showToast({
										title: t.msg,
										icon: 'none'
									})
								}
							});
					}
				},
			
			//点击显示底部弹出框
			clickme: function() {
				this.showModal();
			},

			//显示对话框
			showModal: function() {
				// 显示遮罩层
				var animation = uni.createAnimation({
					transformOrigin: "50% 50%",
					duration: 1000,
					timingFunction: "ease",
					delay: 0
				})

				var animation = uni.createAnimation({
					//duration	Integer 动画持续时间，单位ms
					duration: 200,
					//timingFunction	String  默认"linear"（动画从头到尾的速度是相同的）	定义动画的效果
					timingFunction: 'linear',
					//delay	Integer		默认 0	动画延迟时间，单位 ms
					delay: 0
				})
				animation.translateY('400rpx').step()
				this.animationData = animation.export(),
					setTimeout(() => {
						//隐藏底部栏
						// uni.hideTabBar();
						animation.translateY(0).step(),
							this.animationData = animation.export(),
							this.showModalStatus = true
					}, 100)
			},

			//隐藏对话框
			hideModal: function() {
				// 隐藏遮罩层
				var animation = uni.createAnimation({
					//duration	Integer 动画持续时间，单位ms
					duration: 200,
					//timingFunction	String  默认"linear"（动画从头到尾的速度是相同的）	定义动画的效果
					timingFunction: 'linear',
					//delay	Integer		默认 0	动画延迟时间，单位 ms
					delay: 0
				});
				animation.translateY('400rpx').step(),
					this.animationData = animation.export(),
					setTimeout(() => {
						// 显示底部栏
						uni.showTabBar();

						animation.translateY(0).step(),
							this.showModalStatus = false
					}, 100)
			},
		},
		created(){
			this.clickme()
			this.isShow = this.is_show
			//判断当前是否有登录
			if(uni.getStorageSync('userinfo')){
				this.isShow = false
			}else{
				this.isShow = true
			}
		},
		destroyed(){
			this.isShow = true;
			this.is_show = true;
		}
	}
</script>
<style lang="scss" scoped>
	.login{
		position:fixed;left:0;top:0;z-index:9999;
		background-color: rgba(0,0,0,0.6);
	}
	/*底部框 */
	.bottom_box {
		width: 100%;
		position: fixed;
		bottom: 0;
		left: 0;
		background: #fff;
		border-radius: 8rpx 8rpx,0rpx,0rpx;
	}
		.title {
			width: 100%; 
			height: 51px;
			line-height: 51rpx;
			opacity: 1;
			border-radius: 8px 8px, 0px, 0px;
			background: rgba(232, 243, 255, 1);
			box-shadow: inset 0px -0.5px 0px 0px rgba(229, 230, 235, 1);
			font-size: 16px;
			font-weight: 700;
			color: rgba(29, 33, 41, 1);
			
			.titleText {
				margin:20rpx 30rpx;
				display: inline-block;
				width: 92%;
			}
		}

		.tips_text{
			// border: 1px solid red;
			margin: 0 auto;
			width: 92%;
			height: 300rpx;
		}
		.buttonFooter {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin: 1rpx auto;
			position: fixed;
			bottom: 16px;
		}
		.count {
			text-align: center;
			width: 340rpx;
			padding: 13px 11px;
			border-radius: 2px;
			border-radius: 5rpx;
			margin-left: 5%;
			background: rgba(232, 243, 255, 1);
			font-size: 16px;
			font-weight: 400;
			color: rgba(0, 122, 255, 1);
		}

		.submit {
			text-align: center;
			width: 340rpx;
			height: 84rpx;
			line-height: 44px;
			opacity: 1;
			border-radius: 5rpx;
			background: rgba(22, 93, 255, 1);
			font-size: 16px;
			font-weight: 400;
			color: rgba(255, 255, 255, 1);
			text-align: center;
			margin-left: 12px;
		}
	
	.text{
		margin-top: 50rpx;
		color: #999;
		font-weight: 900;
	}
	
	/*checkbox选中后图标样式  */
	checkbox .wx-checkbox-input.wx-checkbox-input-checked::before {
		width: 30rpx;
		height: 30rpx;
		line-height: 30rpx;
		text-align: center;
		font-size: 26rpx;
		color: #fff;
		background: transparent;
		transform: translate(-50%, -50%) scale(1);
		-webkit-transform: translate(-50%, -50%) scale(1);
	}
</style>